package com.ld.service;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;


@Service
public class MyUserDetailService implements UserDetailsService {

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		if(!"admin".equalsIgnoreCase(username)){
			throw new UsernameNotFoundException("用户名不存在！");
		}
		//1. 指定密码
		String password = "$2a$10$/lJiDjpuIpdXL4NcNvo6CudHhtqEa4tKbUOHmV8OTkTmR7IQ8B9FS";
		//2. 指定当前用户所分配的权限列表
		Collection<? extends GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("admin");
		//3. 返回当前用户
		return new User(username,password,authorities);
	}
}
